<?php
class ProductsController extends AppController {

	public $name = 'Products';
	
	public  function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allow('*');
		$this->setLayout();
	}
	public function index() {
		$this->Product->recursive = 0;
		$this->set('products', $this->paginate());
	}

	function view($companyId = null) {
		if (!$companyId) {
			$this->Session->setFlash(sprintf(__('Invalid %s', true), 'product'));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('product', $this->Product->read(null, $companyId));
	}

	public function addNewProduct() {
		if (!empty($this->data)) {
			$this->Product->create();
			if ($this->Product->save($this->data)) {
				$this->Session->setFlash(__('The product has been saved', true));
                                $pid = $this->Product->id;

                                //update notice when add new company complete
                                $notice = array(
                                    'Notice' =>array(
                                        'user_id' => $this->Bayon->userId(),
                                        'message' => __('add a product ', true).'<strong>'.$this->data['Product']['name'].'</strong> <a href="/products/view/'.$pid.'">'.__('view detail', true).'</a>',
                                        'render' =>  __('add a product ', true).'<strong>'.$this->data['Product']['name'].'</strong> <a href="/products/view/'.$pid.'">'.__('view detail', true).'</a>',
                                        'source' => BAYON_SOURCE_WEB,
                                        'created' => date('Y-m-d h:i:s'),
                                    )
                                );

                            //update to notice front page
                            $this->loadModel('Notice');
                            $this->Notice->addNotice($notice);
                            //update user status
                            $this->loadModel('UserStatus');
                            //add notice counter
                            $this->UserStatus->updateUserStatus($this->Bayon->userId(), STATUS_NOTICE, '+');
                            $first = $this->Session->read($this->Bayon->username().'_first_add_company');
                            if($first){
                                $this->redirect(array('controller' => 'services', 'action' => 'addNewService'));
                            }else{
                               $this->redirect(array('action' => 'index'));
                            }
                            
                            
			} else {
				$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'product'));
			}

		}
		$companies = $this->Product->Company->find('list', array('conditions' => array('Company.user_id' => $this->Bayon->userId())));
		$this->set(compact('companies'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(sprintf(__('Invalid %s', true), 'product'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Product->save($this->data)) {
				$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'product'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'product'));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Product->read(null, $id);
		}
		$companies = $this->Product->Company->find('list');
		$this->set(compact('companies'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(sprintf(__('Invalid id for %s', true), 'product'));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Product->delete($id)) {
			$this->Session->setFlash(sprintf(__('%s deleted', true), 'Product'));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(sprintf(__('%s was not deleted', true), 'Product'));
		$this->redirect(array('action' => 'index'));
	}
	public function admin_index() {
		$this->Product->recursive = 0;
		$this->set('products', $this->paginate());
	}

	public function admin_add() {
		if (!empty($this->data)) {
			$this->Product->create();
			if ($this->Product->save($this->data)) {
				$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'product'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'product'));
			}
		}
		$companies = $this->Product->Company->find('list');
		$this->set(compact('companies'));
	}

	public function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(sprintf(__('Invalid %s', true), 'product'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Product->save($this->data)) {
				$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'product'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'product'));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Product->read(null, $id);
		}
		$companies = $this->Product->Company->find('list');
		$this->set(compact('companies'));
	}

	public function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(sprintf(__('Invalid id for %s', true), 'product'));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Product->delete($id)) {
			$this->Session->setFlash(sprintf(__('%s deleted', true), 'Product'));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(sprintf(__('%s was not deleted', true), 'Product'));
		$this->redirect(array('action' => 'index'));
	}
}
?>